package cn.green.core.excel.model.impls;

import cn.green.core.components.field.FieldFormTypeCom;
import cn.green.core.components.store.field.FieldFormTypeStore;
import cn.green.core.excel.model.Column;
import cn.green.core.model.QField;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
//import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.util.*;

/**
 * @author wang.yishu
 * @version 1.0
 * @className DateColumn
 * @description 日期单元格
 * @date 2025/1/6 10:24
 */
public class DateColumn extends Column {

    public DateColumn(Integer index, QField field) {
        super(index, field);
        String fieldForm = field.getFieldForm();
        if (fieldForm != null && !"".equals(fieldForm)) {
            FieldFormTypeCom fieldFormTypeCom = FieldFormTypeStore.get(fieldForm);
            if (fieldFormTypeCom != null) {
                this.columnName = this.columnName + "(" + fieldFormTypeCom.patternName() + ")";
            }
        }
    }

    @Override
    public List<HSSFCell> generateXlsTitle(HSSFWorkbook workbook, HSSFSheet sheet, HSSFRow row) {
        HSSFCell cell = row.createCell(this.index);
        cell.setCellType(HSSFCell.CELL_TYPE_STRING);
        if (this.xlsTitleCellStyle != null) {
            cell.setCellStyle(this.xlsTitleCellStyle);
        }
        cell.setCellValue(this.columnName);
        return Collections.singletonList(cell);
    }

    @Override
    public List<XSSFCell> generateXlsxTitle(XSSFWorkbook workbook, XSSFSheet sheet, XSSFRow row) {
        XSSFCell cell = row.createCell(this.index);
        cell.setCellType(HSSFCell.CELL_TYPE_STRING);
        if (this.xlsxTitleCellStyle != null) {
            cell.setCellStyle(this.xlsxTitleCellStyle);
        }
        cell.setCellValue(this.columnName);
        return Collections.singletonList(cell);
    }

    @Override
    public List<HSSFCell> generateXlsCell(HSSFWorkbook workbook, HSSFSheet sheet, HSSFRow row, Object cellVal) {
        HSSFCell cell = row.createCell(this.index);
        cell.setCellType(HSSFCell.CELL_TYPE_STRING);
        if (cellVal != null) {
            if (cellVal instanceof String) {
                cell.setCellValue(cellVal.toString());
            } else if (cellVal instanceof Date && this.field != null) {
                String fieldForm = this.field.getFieldForm();
                if (fieldForm != null && !fieldForm.isEmpty()) {
                    FieldFormTypeCom fieldFormTypeCom = FieldFormTypeStore.get(fieldForm);
                    Object format = fieldFormTypeCom.format(cellVal);
                    if (format != null) {
                        cell.setCellValue(format.toString());
                    }
                }
            }
        } else {
            cell.setCellValue("");
        }
        return Collections.singletonList(cell);
    }

    @Override
    public List<XSSFCell> generateXlsxCell(XSSFWorkbook workbook, XSSFSheet sheet, XSSFRow row, Object cellVal) {
        XSSFCell cell = row.createCell(this.index);
        cell.setCellType(HSSFCell.CELL_TYPE_STRING);
        if (cellVal != null) {
            if (cellVal instanceof String) {
                cell.setCellValue(cellVal.toString());
            } else if (cellVal instanceof Date && this.field != null) {
                String fieldForm = this.field.getFieldForm();
                if (fieldForm != null && !fieldForm.isEmpty()) {
                    FieldFormTypeCom fieldFormTypeCom = FieldFormTypeStore.get(fieldForm);
                    Object format = fieldFormTypeCom.format(cellVal);
                    if (format != null) {
                        cell.setCellValue(format.toString());
                    }
                }
            }
        }
        return Collections.singletonList(cell);
    }

    @Override
    public Map<String, Object> getValueFromXlsCell(Map<String, HSSFCell> cells) {
        Map<String, Object> data = new HashMap<String, Object>();
        cells.forEach((fieldName, cell) -> {
            if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
                String cellValue = cell.getStringCellValue();
                if (cellValue != null) {
                    String fieldForm = field.getFieldForm();
                    if (fieldForm != null && !fieldForm.isEmpty()) {
                        FieldFormTypeCom fieldFormTypeCom = FieldFormTypeStore.get(fieldForm);
                        data.put(fieldName, fieldFormTypeCom.parse(cellValue));
                    }
                }
            } else if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
                data.put(fieldName, cell.getDateCellValue());
            }
        });
        return data;
    }

    @Override
    public Map<String, Object> getValueFromXlsxCell(Map<String, XSSFCell> cells) {
        Map<String, Object> data = new HashMap<String, Object>();
        cells.forEach((fieldName, cell) -> {
            if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
                String cellValue = cell.getStringCellValue();
                if (cellValue != null) {
                    String fieldForm = field.getFieldForm();
                    if (fieldForm != null && !fieldForm.isEmpty()) {
                        FieldFormTypeCom fieldFormTypeCom = FieldFormTypeStore.get(fieldForm);
                        data.put(fieldName, fieldFormTypeCom.parse(cellValue));
                    }
                }
            } else if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
                data.put(fieldName, cell.getDateCellValue());
            }
        });
        return data;
    }
}
